package com.fanshuai.hash;

import java.util.Iterator;

public class ConcurrentHashMap implements HashTable {
    public static class Node {
        public String key;
        public int value;
        public Node next;

        public Node(String key, int value) {
            this.key = key;
            this.value = value;
            this.next = null;
        }
    }

    private Node[] table;
    private int size;

    private int defaultSize = 16;
    private double loadFactor = 0.75;
    private int LIMIT = 4 * 1024 * 1024;

    /**
     * lock，用于resize
     */
    private Object lock = new Object();

    public ConcurrentHashMap() {
        table = new Node[defaultSize];
        size = 0;
    }

    private void resize() {
        int newSize = calcSize();
        Node[] newTable = new Node[newSize];

        synchronized (lock) {
            for (int i = 0; i < table.length; i++) {
                if (table[i] == null) {
                    continue;
                }

                Node cur = table[i];
                while (cur != null) {
                    Node newNode = new Node(cur.key, cur.value);
                    int newIndex = cur.key.hashCode() % newSize;

                    Node last = newTable[newIndex];
                    if (last == null) {
                        newTable[newIndex] = newNode;
                    } else {
                        while (last.next != null) {
                            last = last.next;
                        }
                        last.next = newNode;
                    }


                    cur = cur.next;
                }
            }
        }

        table = newTable;
    }

    private int calcSize() {
        if (2 * table.length < LIMIT) {
            return 2 * table.length;
        } else {
            return table.length + LIMIT;
        }
    }

    @Override
    public void insert(String key, int value) {
        if ((size + 1.0) / table.length >= loadFactor) {
            resize();
        }

        int index = key.hashCode() % table.length;
        Node node = new Node(key, value);
        synchronized (table[index]) {
            Node last = table[index];
            if (last == null) {
                table[index] = node;
            } else {
                while (last.next != null) {
                    last = last.next;
                }
                last.next = node;
            }

            size++;
        }
    }

    @Override
    public Integer find(String key) {
        int index = key.hashCode() % table.length;
        synchronized (table[index]) {
            Node cur = table[index];
            while (cur != null && !cur.key.equals(key)) {
                cur = cur.next;
            }

            if (cur != null) {
                return cur.value;
            } else {
                return null;
            }
        }
    }

    @Override
    public void delete(String key) {
        int index = key.hashCode() % table.length;
        synchronized (table[index]) {
            Node node = table[index];
            Node pre = table[index];

            while (node != null && !node.key.equals(key)) {
                pre = node;
                node = node.next;
            }

            if (node != null) {
                pre.next = node.next;
                node.next = null;
                node = null;
                size--;
            }
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean contains(String key) {
        return find(key) != null;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int index = 0;
            int visitedSize = 0;
            Node cur = null;

            @Override
            public boolean hasNext() {
                return visitedSize < size;
            }

            @Override
            public Integer next() {
                synchronized (lock) {
                    if (cur == null) {
                        while (table[index] == null && index < table.length) {
                            index++;
                        }

                        cur = table[index];
                    }

                    int value = cur.value;
                    cur = cur.next;
                    visitedSize++;
                    return value;
                }
            }
        };
    }
}
